package com.android.firstgame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.DragEvent;
import android.graphics.Paint;
import java.lang.Runnable;
import java.lang.Thread;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import java.util.ArrayList;

public class DrawView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
    private Paint paint = new Paint();
	private float f_pointerx = 0;
	private float f_pointery = 0;
	//danh.ngothanh: for draw debug string
	private int i_debug_string_posy = -5;
	//danh.ngothanh: for control vpad
	private float i_move_small_circle_x;
	private float i_move_small_circle_y;
	private float i_fight_small_circle_x;
	private float i_fight_small_circle_y;
	private static float I_STATIC_MOVE_SMALL_CIRCLE_X;
	private static float I_STATIC_MOVE_SMALL_CIRCLE_Y;
	private static float I_STATIC_FIGHT_SMALL_CIRCLE_X;
	private static float I_STATIC_FIGHT_SMALL_CIRCLE_Y;
	private static int I_STATIC_SMALL_CIRCLE_RADIUS;
	//danh.ngothanh: for screen	
	private static int I_SCREEN_WIDTH;
	private static int I_SCREEN_HEIGHT;
	//danh.ngothanh: for MC
	private float f_mc_posx, f_mc_posy;
	private float f_gun_posx, f_gun_posy;
	private int i_mc_radius, i_mc_gun_length;
	private double f_move_angle, f_gun_angle;
	private float f_move_touched_posx, f_move_touched_posy, f_fight_touched_posx, f_fight_touched_posy;
	//danh.ngothanh: for input
	private boolean b_move_touched, b_fight_touched;
	private int i_touched_pointer_count;
	private int i_move_touched_pointerid, i_fight_touched_pointerid;
	private int i_move_touched_pointerindex, i_fight_touched_pointerindex;
	private double f_move_distance_length, f_fight_distance_length;
	//danh.ngothanh: for debug
	private int[] input_pointer_array, input_pointer_array_id;
	private float[] input_pointer_array_x, input_pointer_array_y;
	
	//danh.ngothanh: for bullets
    private ArrayList<Bullet> bulletlist;
	private float f_gun_virtual_target_x, f_gun_virtual_target_y;
	private int i_bullet_delay;
	
	//danh.ngothanh: for background
	private int[] i_background_vertical_lines_x;
	private int[] i_background_horizontal_lines_y;
	private static int I_MAX_VERTICAL_LINE, I_MAX_HORIZONTAL_LINE;
	
	//danh.ngothanh: holder
	private SurfaceHolder o_surfaceholder;
	//danh.ngothanh: main game thread
	private Thread o_game_mainthread;
	private boolean i_thread_can_run = false;
	private boolean is_set_move = false, is_set_fight = false;
	
	
    public DrawView(Context context){
        super(context);
		o_surfaceholder = getHolder();
		o_surfaceholder.addCallback(this);
		setFocusable(true);
		I_SCREEN_WIDTH = context.getResources().getDisplayMetrics().widthPixels;
		I_SCREEN_HEIGHT = context.getResources().getDisplayMetrics().heightPixels;
    }
	
	public void Initialize(){
		// danh.ngothanh: ini control
		I_STATIC_SMALL_CIRCLE_RADIUS = I_SCREEN_WIDTH/32;
		I_STATIC_MOVE_SMALL_CIRCLE_X = i_move_small_circle_x = I_SCREEN_WIDTH/6;
		I_STATIC_FIGHT_SMALL_CIRCLE_X = i_fight_small_circle_x  = I_SCREEN_WIDTH - I_SCREEN_WIDTH/6;
		I_STATIC_MOVE_SMALL_CIRCLE_Y = i_move_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y = i_fight_small_circle_y = I_SCREEN_HEIGHT - I_SCREEN_HEIGHT/4;		
		//danh.ngothanh: ini MC
		f_mc_posx = I_SCREEN_WIDTH/2;
		f_mc_posy = I_SCREEN_HEIGHT/2;
		i_mc_radius = I_SCREEN_WIDTH/64;
		i_mc_gun_length = i_mc_radius + i_mc_radius/2;
		f_gun_angle = Math.toRadians(180);
		f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
		f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
		f_move_angle = Math.atan2(i_move_small_circle_x - I_STATIC_MOVE_SMALL_CIRCLE_X, i_move_small_circle_y - I_STATIC_MOVE_SMALL_CIRCLE_Y);
		//danh.ngothanh: ini input
		b_move_touched = false;
		b_fight_touched = false;
		i_touched_pointer_count = 0;
		f_move_touched_posx = -1;
		f_move_touched_posy = -1;
		f_fight_touched_posx = -1;
		f_fight_touched_posy = -1;	
		i_move_touched_pointerindex = -1;
		i_fight_touched_pointerindex = -1;
		i_move_touched_pointerid = -1;
		i_fight_touched_pointerid = -1;
		//danh.ngothanh: for debug
		input_pointer_array = new int[1];
		input_pointer_array_id = new int[1];
		input_pointer_array_x = new float[1];
		input_pointer_array_y = new float[1];
		//danh.ngothanh: for bullets
		bulletlist = new ArrayList<Bullet>();
		f_gun_virtual_target_x = 0;
		f_gun_virtual_target_y = 0;
		i_bullet_delay = 2;
		
		//danh.ngothanh: for background
		I_MAX_VERTICAL_LINE = 5;
		I_MAX_HORIZONTAL_LINE = I_SCREEN_HEIGHT/(I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
		i_background_vertical_lines_x = new int[I_MAX_VERTICAL_LINE];
		i_background_horizontal_lines_y = new int[I_MAX_HORIZONTAL_LINE];
		i_background_vertical_lines_x[0] = (int)I_SCREEN_WIDTH/(2*I_MAX_VERTICAL_LINE);
		i_background_horizontal_lines_y[0] = (int)I_SCREEN_HEIGHT/(2*I_MAX_HORIZONTAL_LINE);
		for(int i = 1; i < I_MAX_VERTICAL_LINE; i++){
			i_background_vertical_lines_x[i] = i_background_vertical_lines_x[i-1] + (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
		}
		for(int i = 1; i < I_MAX_HORIZONTAL_LINE; i++){
			i_background_horizontal_lines_y[i] = i_background_horizontal_lines_y[i-1] + (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
		}		
		//danh.ngothanh: game main thread
		o_game_mainthread = new Thread(this);
		o_game_mainthread.start();
		i_thread_can_run = true;
	}

    @Override
    protected void onDraw(Canvas canvas){
		i_debug_string_posy = 0;
        paint.setColor(Color.BLACK);
        canvas.drawRect(0, 0, I_SCREEN_WIDTH, I_SCREEN_HEIGHT, paint);
		paint.setColor(Color.GREEN);
		for(int i = 0; i < I_MAX_VERTICAL_LINE; i ++){
			canvas.drawLine(i_background_vertical_lines_x[i], 0, i_background_vertical_lines_x[i], I_SCREEN_HEIGHT, paint);
		}
		for(int i = 0; i < I_MAX_HORIZONTAL_LINE; i++){
			canvas.drawLine(0, i_background_horizontal_lines_y[i], I_SCREEN_WIDTH, i_background_horizontal_lines_y[i], paint);
		}
		//danh.ngothanh: move control
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		canvas.drawCircle(I_STATIC_MOVE_SMALL_CIRCLE_X, I_STATIC_MOVE_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
		paint.setStyle(Paint.Style.STROKE);//danh.ngothanh: set style for paint stroke only
		paint.setColor(Color.GREEN);
		canvas.drawCircle(I_STATIC_MOVE_SMALL_CIRCLE_X, I_STATIC_MOVE_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);		
		paint.setColor(Color.GRAY);
		canvas.drawCircle(I_STATIC_MOVE_SMALL_CIRCLE_X, I_STATIC_MOVE_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
		paint.setColor(Color.YELLOW);
		canvas.drawCircle(i_move_small_circle_x, i_move_small_circle_y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);		
		//danh.ngothanh: fight control
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		canvas.drawCircle(I_STATIC_FIGHT_SMALL_CIRCLE_X, I_STATIC_FIGHT_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(Color.BLUE);
		canvas.drawCircle(I_STATIC_FIGHT_SMALL_CIRCLE_X, I_STATIC_FIGHT_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);
		paint.setColor(Color.GRAY);
		canvas.drawCircle(I_STATIC_FIGHT_SMALL_CIRCLE_X, I_STATIC_FIGHT_SMALL_CIRCLE_Y, I_STATIC_SMALL_CIRCLE_RADIUS*2, paint);
		paint.setColor(Color.RED);
		canvas.drawCircle(i_fight_small_circle_x, i_fight_small_circle_y, I_STATIC_SMALL_CIRCLE_RADIUS, paint);		
		paint.setStyle(Paint.Style.FILL);//danh.ngothanh: reset style for fill shape
		//danh.ngothanh: draw MC
		paint.setColor(Color.WHITE);
		canvas.drawCircle(f_mc_posx, f_mc_posy, i_mc_radius, paint);
		paint.setStrokeWidth(3f);
		canvas.drawLine(f_mc_posx, f_mc_posy, f_gun_posx, f_gun_posy, paint);
		paint.setStrokeWidth(1f);
		
		//draw bullet
        try{
			for(int i = 0; i < bulletlist.size(); i ++)
				canvas.drawRect(bulletlist.get(i).getBulletX() - 1, bulletlist.get(i).getBulletY() - 1, bulletlist.get(i).getBulletX() + 1, bulletlist.get(i).getBulletY() + 1, paint);
        }catch(Exception e){}
		
		//danh.ngothanh: draw debug string
		paint.setColor(Color.WHITE);
		// canvas.drawText("X: " + f_pointerx, 5, i_debug_string_posy += 15, paint);
		// canvas.drawText("Y: " + f_pointery, 5, i_debug_string_posy += 15, paint);
		// canvas.drawText("Width: " + I_SCREEN_WIDTH, 5, i_debug_string_posy += 15, paint);
		// canvas.drawText("Height: " + I_SCREEN_HEIGHT, 5, i_debug_string_posy += 15, paint);		
		canvas.drawText("Pointer Count: " + i_touched_pointer_count, 5,  i_debug_string_posy+= 15, paint);
		canvas.drawText("Move: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid + ", " + (int)f_move_touched_posx + ", " + (int)f_move_touched_posy, 5,  i_debug_string_posy+= 15, paint);
		canvas.drawText("Fight: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid + ", " + (int)f_fight_touched_posx + ", " + (int)f_fight_touched_posy, 5,  i_debug_string_posy+= 15, paint);			
		// canvas.drawText("when down: "+ pointercountwhenpointerdown, 5, i_debug_string_posy+= 15, paint);
		// canvas.drawText("down time: "+ downtime, 5, i_debug_string_posy+= 15, paint);
		if(i_touched_pointer_count > 0){
			for(int i = 0; i < i_touched_pointer_count; i ++){
				canvas.drawText("Index: " + i + ", ID: " + input_pointer_array_id[i] + ", X: " + (int)input_pointer_array_x[i] + ", Y: " + (int)input_pointer_array_y[i], 5,  i_debug_string_posy+= 15, paint);
			}
		}		
		// canvas.drawText("ID1: " + i_move_touched_pointerid, 5,  i_debug_string_posy+= 15, paint);
		// canvas.drawText("ID2: " + i_fight_touched_pointerid, 5,  i_debug_string_posy+= 15, paint);
		// canvas.drawText("Index1: " + i_move_touched_pointerindex, 5,  i_debug_string_posy+= 15, paint);
		// canvas.drawText("Index2: " + i_fight_touched_pointerindex, 5,  i_debug_string_posy+= 15, paint);
		if(is_set_move){
			is_set_move = false;
			canvas.drawText("Is set move", 5, i_debug_string_posy+= 15, paint);
		}
		if(is_set_fight){
			is_set_fight = false;
			canvas.drawText("Is set fight", 5, i_debug_string_posy+= 15, paint);
		}
    }
	
	public void Game_Update(){
		//calculate for gun
		for(int i = 0; i < bulletlist.size(); i ++){
			if(bulletlist.get(i).checkvisible() != true){
				try {
					bulletlist.get(i).finalize();
				} catch (Throwable ex) {
					ex.printStackTrace();
				}
				bulletlist.remove(i);
			}
		}
		try{
			for(int i = 0; i < bulletlist.size(); i ++){
				bulletlist.get(i).relocation();
			}
		}
		catch(Exception e){}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		i_touched_pointer_count = e.getPointerCount();
		if(e.getActionMasked() == MotionEvent.ACTION_UP || e.getActionMasked() == MotionEvent.ACTION_POINTER_UP){
			// System.out.println("MotionEvent.ACTION_UP ============================= " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
			if(e.getActionIndex() == e.findPointerIndex(i_move_touched_pointerid)){			
				// System.out.println("Move releaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
				//find new pointer ID if available
				int i_old_move_touched_pointerid = i_move_touched_pointerid;
				for(int i = 0; i < i_touched_pointer_count; i++){
					if(e.getX(i) < I_SCREEN_WIDTH/2 && e.getPointerId(i) != i_old_move_touched_pointerid){
						i_move_touched_pointerindex = i;
						i_move_touched_pointerid = e.getPointerId(i);
						System.out.println("Found new move control " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
						break;
					}
					i_move_touched_pointerid = -1;
					i_move_touched_pointerindex = -1;
								
				}	
				
				// System.out.println("Move Index1: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
				// System.out.println("Fight Index1: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
				if(i_touched_pointer_count == 1)
					i_touched_pointer_count = 0;
			}			
			if(e.getActionIndex() == e.findPointerIndex(i_fight_touched_pointerid)){			
				// System.out.println("Fight releaseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
				//find new pointer ID if available
				int i_old_fight_touched_pointerid = i_fight_touched_pointerid;
				for(int i = 0; i < i_touched_pointer_count; i++){
					if(e.getX(i) >= I_SCREEN_WIDTH/2 && e.getPointerId(i) != i_old_fight_touched_pointerid){
						System.out.println("Found new fight control 1 " + i + ", " + e.getPointerId(i));
						System.out.println("Found new fight control 2 " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
						i_fight_touched_pointerindex = i;
						i_fight_touched_pointerid = e.getPointerId(i);
						break;
					}
					i_fight_touched_pointerid = -1;
					i_fight_touched_pointerindex = -1;
				}	
				
				// System.out.println("Move Index2: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
				// System.out.println("Fight Index2: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
			}
		}
		
		if(e.getActionMasked() == MotionEvent.ACTION_DOWN || e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
			// System.out.println("MotionEvent.ACTION_DOWN ============================= " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
			if(e.getX(e.getActionIndex()) < I_SCREEN_WIDTH/2 && i_move_touched_pointerid == -1){
				i_move_touched_pointerindex = e.getActionIndex();
				i_move_touched_pointerid = e.getPointerId(i_move_touched_pointerindex);
				// System.out.println("Moveeeeeeeeee " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
			}			
			if(e.getX(e.getActionIndex()) >= I_SCREEN_WIDTH/2 && i_fight_touched_pointerid == -1){
				i_fight_touched_pointerindex = e.getActionIndex();
				i_fight_touched_pointerid = e.getPointerId(i_fight_touched_pointerindex);
				// System.out.println("Fightttttttt " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
			}
		}
		if(e.getActionMasked() == MotionEvent.ACTION_MOVE){
			if(i_move_touched_pointerid != -1){
				// System.out.println("Move controlingggggggggggggggggggggggggggggggggggggg");
				//refresh pointer index
				i_move_touched_pointerindex = e.findPointerIndex(i_move_touched_pointerid);
				// System.out.println("Move Index 3: " + i_move_touched_pointerindex + ", " + i_move_touched_pointerid);
				// System.out.println("MotionEvent.ACTION_MOVE 1 " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
				f_move_touched_posx = e.getX(i_move_touched_pointerindex);
				f_move_touched_posy = e.getY(i_move_touched_pointerindex);
				f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));
				if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
					f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
				}
				f_move_angle = Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y);
				i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(f_move_angle));
				i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(f_move_angle));
				//old design
				// f_mc_posx += (float)(f_move_distance_length/8*Math.sin(f_move_angle));
				// f_mc_posy += (float)(f_move_distance_length/8*Math.cos(f_move_angle));
				//danh.ngothanh: caculate background move
				i_background_vertical_lines_x[0] -= (int)(f_move_distance_length/8*Math.sin(f_move_angle));//danh.ngothanh: vi la background di chuyen nen tru chu ko phai cong nhu mc
				i_background_horizontal_lines_y[0] -= (int)(f_move_distance_length/8*Math.cos(f_move_angle));//danh.ngothanh: tuong tu nhu tren
				if(i_background_vertical_lines_x[0] < 0){
					i_background_vertical_lines_x[0] = (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE) + i_background_vertical_lines_x[0];
				}else if(i_background_vertical_lines_x[0] > (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE)){
					i_background_vertical_lines_x[0] = i_background_vertical_lines_x[0] - (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
				}
				if(i_background_horizontal_lines_y[0] < 0){
					i_background_horizontal_lines_y[0] = (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE) + i_background_horizontal_lines_y[0];
				}else if(i_background_horizontal_lines_y[0] > (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE)){
					i_background_horizontal_lines_y[0] = i_background_horizontal_lines_y[0] - (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
				}
				for(int j = 1; j < I_MAX_VERTICAL_LINE; j++){
					i_background_vertical_lines_x[j] = i_background_vertical_lines_x[j-1] + (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
				}
				for(int j = 1; j < I_MAX_HORIZONTAL_LINE; j++){
					i_background_horizontal_lines_y[j] = i_background_horizontal_lines_y[j-1] + (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
				}
				//old design
				// f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
				// f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
			}
			if(i_fight_touched_pointerid != -1){
				// System.out.println("Fight controlingggggggggggggggggggggggggggggggggggggg");
				//refresh pointer index
				i_fight_touched_pointerindex = e.findPointerIndex(i_fight_touched_pointerid);
				// System.out.println("Fight Index 3: " + i_fight_touched_pointerindex + ", " + i_fight_touched_pointerid);
				// System.out.println("MotionEvent.ACTION_MOVE 2 " + e.getActionIndex() + ", " + e.getPointerId(e.getActionIndex()));
				f_fight_touched_posx = e.getX(i_fight_touched_pointerindex);
				f_fight_touched_posy = e.getY(i_fight_touched_pointerindex);
				f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
				if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
					f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
				}
				f_gun_angle = Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y);
				i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(f_gun_angle));
				i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(f_gun_angle));
				f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
				f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
				f_gun_virtual_target_x = (float)((double)f_mc_posx + 3*i_mc_gun_length*Math.sin(f_gun_angle));
				f_gun_virtual_target_y = (float)((double)f_mc_posy + 3*i_mc_gun_length*Math.cos(f_gun_angle));
				bulletlist.add(new Bullet(f_gun_posx, f_gun_posy, f_gun_virtual_target_x, f_gun_virtual_target_y));
			}
			
		}
		// f_pointerx = e.getX();
		// f_pointery = e.getY();
		/*
		i_touched_pointer_count = e.getPointerCount();
		i_pointer_count = i_touched_pointer_count;
		for(int i = 0; i < i_touched_pointer_count; i++){
			// int action = e.getActionMasked();
			int action = e.getAction() & MotionEvent.ACTION_MASK;
			switch (action)
    		{
    			case MotionEvent.ACTION_POINTER_DOWN:
    				// if(e.getX(i) < I_SCREEN_WIDTH/2 && i_move_touched_pointerid == -1){
						// i_move_touched_pointerid = e.getPointerId(i);
						// i_move_touched_pointerindex = e.getActionIndex();
						// b_move_touched = true;
					// }else if(e.getX(i) >= I_SCREEN_WIDTH/2 && i_fight_touched_pointerid == -1){
						// i_fight_touched_pointerid = e.getPointerId(i);
						// i_fight_touched_pointerindex = e.getActionIndex();
						// b_fight_touched = true;
					// }
    				
					System.out.println("Downnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn" + i);
    				if(e.getX(i) < I_SCREEN_WIDTH/2){
						// i_move_touched_pointerid = e.getPointerId(i);
						// i_move_touched_pointerindex = e.findPointerIndex(i_move_touched_pointerid);
						f_move_touched_posx = e.getX(i);
						f_move_touched_posy = e.getY(i);				
						f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));				
						if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						}
						f_move_angle = Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y);
						i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(f_move_angle));
						i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(f_move_angle));									
						f_mc_posx += (float)(f_move_distance_length/8*Math.sin(f_move_angle));
						f_mc_posy += (float)(f_move_distance_length/8*Math.cos(f_move_angle));
						f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
						f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
						b_move_touched = true;
					}
					if(e.getX(i) >= I_SCREEN_WIDTH/2){
						// i_fight_touched_pointerid = e.getPointerId(i);
						// i_fight_touched_pointerindex = e.findPointerIndex(i_fight_touched_pointerid);	
						f_fight_touched_posx = e.getX(i);
						f_fight_touched_posy = e.getY(i);
						f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
						if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						}
						f_gun_angle = Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y);
						i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(f_gun_angle));
						i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(f_gun_angle));
						f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
						f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
						f_gun_virtual_target_x = (float)((double)f_mc_posx + 3*i_mc_gun_length*Math.sin(f_gun_angle));
						f_gun_virtual_target_y = (float)((double)f_mc_posy + 3*i_mc_gun_length*Math.cos(f_gun_angle));												
						bulletlist.add(new Bullet(f_gun_posx, f_gun_posy, f_gun_virtual_target_x, f_gun_virtual_target_y));						
						b_fight_touched = true;
					}
    				break;
    			case MotionEvent.ACTION_UP:
					// System.out.println("Upppppppppppppppppppppppppppppppppppppppp " + i);
					// if(i_touched_pointer_count == 1)
						// i_touched_pointer_count = 0;
    				// if(i_move_touched_pointerid == e.getPointerId(i)){
						// i_move_touched_pointerid = -1;
						// i_move_touched_pointerindex = -1;
						// f_move_touched_posx = -1;
						// f_move_touched_posy = -1;						
						// i_move_small_circle_x = I_STATIC_MOVE_SMALL_CIRCLE_X;
						// i_move_small_circle_y = I_STATIC_MOVE_SMALL_CIRCLE_Y;
						// b_move_touched = false;
					// }
					// if(i_fight_touched_pointerid == e.getPointerId(i)){
						// i_fight_touched_pointerid = -1;
						// i_fight_touched_pointerindex = -1;
						// f_fight_touched_posx = -1;
						// f_fight_touched_posy = -1;						
						// i_fight_small_circle_x = I_STATIC_FIGHT_SMALL_CIRCLE_X;
						// i_fight_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y;
						// b_fight_touched = false;
					// }
    					// if(i_move_touched_pointerid == e.getPointerId(i)){
					if(e.getX(i) < I_SCREEN_WIDTH/2){
						// i_move_touched_pointerid = -1;
						// i_move_touched_pointerindex = -1;
						f_move_touched_posx = -1;
						f_move_touched_posy = -1;						
						i_move_small_circle_x = I_STATIC_MOVE_SMALL_CIRCLE_X;
						i_move_small_circle_y = I_STATIC_MOVE_SMALL_CIRCLE_Y;
						// b_move_touched = false;
					}
					// if(i_fight_touched_pointerid == e.getPointerId(i)){
					if(e.getX(i) >= I_SCREEN_WIDTH/2){
						// i_fight_touched_pointerid = -1;
						// i_fight_touched_pointerindex = -1;
						f_fight_touched_posx = -1;
						f_fight_touched_posy = -1;						
						i_fight_small_circle_x = I_STATIC_FIGHT_SMALL_CIRCLE_X;
						i_fight_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y;
						// b_fight_touched = false;
					}
					break;	
    			// case MotionEvent.ACTION_POINTER_DOWN:
    				
    				// break;
    			// case MotionEvent.ACTION_POINTER_UP:
        			
        			// break;
    			case MotionEvent.ACTION_MOVE:
    				// if(i_move_touched_pointerid == e.getPointerId(i)){
						// f_move_touched_posx = e.getX(i);
						// f_move_touched_posy = e.getY(i);				
						// f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));				
						// if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							// f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						// }
						// f_move_angle = Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y);
						// i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(f_move_angle));
						// i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(f_move_angle));									
						// f_mc_posx += (float)(f_move_distance_length/7*Math.sin(f_move_angle));
						// f_mc_posy += (float)(f_move_distance_length/7*Math.cos(f_move_angle));
						// f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
						// f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
					// }
					// if(i_fight_touched_pointerid == e.getPointerId(i)){
						// f_fight_touched_posx = e.getX(i);
						// f_fight_touched_posy = e.getY(i);				
						// f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
						// if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							// f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						// }
						// f_gun_angle = Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y);
						// i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(f_gun_angle));
						// i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(f_gun_angle));
						// f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
						// f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
					// }
    				System.out.println("Moveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee " + i);
					if(e.getX(i) < I_SCREEN_WIDTH/2){
						// b_move_touched = true;
						// i_move_touched_pointerindex = e.findPointerIndex(i);
						f_move_touched_posx = e.getX(i);
						f_move_touched_posy = e.getY(i);				
						f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));				
						if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						}
						f_move_angle = Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y);
						i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(f_move_angle));
						i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(f_move_angle));															
						// f_mc_posx += (float)(f_move_distance_length/8*Math.sin(f_move_angle));						
						// f_mc_posy += (float)(f_move_distance_length/8*Math.cos(f_move_angle));
						//danh.ngothanh: caculate background move
						i_background_vertical_lines_x[0] -= (int)(f_move_distance_length/8*Math.sin(f_move_angle));//danh.ngothanh: vi la background di chuyen nen tru chu ko phai cong nhu mc
						i_background_horizontal_lines_y[0] -= (int)(f_move_distance_length/8*Math.cos(f_move_angle));//danh.ngothanh: tuong tu nhu tren
						if(i_background_vertical_lines_x[0] < 0){
							i_background_vertical_lines_x[0] = (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE) + i_background_vertical_lines_x[0];
						}else if(i_background_vertical_lines_x[0] > (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE)){
							i_background_vertical_lines_x[0] = i_background_vertical_lines_x[0] - (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
						}
						if(i_background_horizontal_lines_y[0] < 0){
							i_background_horizontal_lines_y[0] = (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE) + i_background_horizontal_lines_y[0];
						}else if(i_background_horizontal_lines_y[0] > (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE)){
							i_background_horizontal_lines_y[0] = i_background_horizontal_lines_y[0] - (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
						}
						for(int j = 1; j < I_MAX_VERTICAL_LINE; j++){
							i_background_vertical_lines_x[j] = i_background_vertical_lines_x[j-1] + (I_SCREEN_WIDTH/I_MAX_VERTICAL_LINE);
						}
						for(int j = 1; j < I_MAX_HORIZONTAL_LINE; j++){
							i_background_horizontal_lines_y[j] = i_background_horizontal_lines_y[j-1] + (I_SCREEN_HEIGHT/I_MAX_HORIZONTAL_LINE);
						}
						// f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
						// f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
					}
					if(e.getX(i) >= I_SCREEN_WIDTH/2){
						// b_fight_touched = true;
						// i_fight_touched_pointerindex = e.findPointerIndex(i_fight_touched_pointerid);
						f_fight_touched_posx = e.getX(i);
						f_fight_touched_posy = e.getY(i);
						f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
						if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
							f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
						}
						f_gun_angle = Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y);
						i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(f_gun_angle));
						i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(f_gun_angle));
						f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
						f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
						f_gun_virtual_target_x = (float)((double)f_mc_posx + 3*i_mc_gun_length*Math.sin(f_gun_angle));
						f_gun_virtual_target_y = (float)((double)f_mc_posy + 3*i_mc_gun_length*Math.cos(f_gun_angle));
						bulletlist.add(new Bullet(f_gun_posx, f_gun_posy, f_gun_virtual_target_x, f_gun_virtual_target_y));
					}
					break;
    			default:
    				break;
    		}
			if(b_move_touched && b_fight_touched)
				break;
		}
		input_pointer_array = new int[i_touched_pointer_count];
		input_pointer_array_id = new int[i_touched_pointer_count];
		input_pointer_array_x = new float[i_touched_pointer_count];
		input_pointer_array_y = new float[i_touched_pointer_count];
		for(int i = 0; i < i_touched_pointer_count; i ++){
			input_pointer_array_id[i] = e.getPointerId(i);
			input_pointer_array_x[i] = e.getX(i);
			input_pointer_array_y[i] = e.getY(i);			
		}
		*/
		/*
		if(e.getActionMasked() == MotionEvent.ACTION_UP){
			pointercountwhenpointerdown = 0;
			if(i_touched_pointer_count == 1)
				i_touched_pointer_count = 0;
			if(i_move_touched_pointerid == e.getPointerId(e.getActionIndex())){
				//find new move control if avalable
				i_move_touched_pointerid = -1;
				i_move_touched_pointerindex = -1;
				f_move_touched_posx = -1;
				f_move_touched_posy = -1;
				i_move_small_circle_x = I_STATIC_MOVE_SMALL_CIRCLE_X;
				i_move_small_circle_y = I_STATIC_MOVE_SMALL_CIRCLE_Y;
			}
			
			if(i_fight_touched_pointerid == e.getPointerId(e.getActionIndex())){
				//find new fight control if avalable
				i_fight_touched_pointerid = -1;
				i_fight_touched_pointerindex = -1;
				f_fight_touched_posx = -1;
				f_fight_touched_posy = -1;
				i_fight_small_circle_x = I_STATIC_FIGHT_SMALL_CIRCLE_X;
				i_fight_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y;
				fightactionindex = -1;
				fightactionid = -1;
			}
			
			if(e.getX() < I_SCREEN_WIDTH/2){
				if(e.getActionIndex() == i_move_touched_pointerid){
					//find new control if avalable
					b_move_touched = false;
					for(int i = 0; i < i_touched_pointer_count; i ++){
						if((e.getX(i) < I_SCREEN_WIDTH/2) && (e.getActionIndex() != i_move_touched_pointerid))
						i_move_touched_pointerindex	= e.getActionIndex();
						b_move_touched = true;
						break;
					}
				}
			}else{
				if(e.getActionIndex() == i_fight_touched_pointerindex){
					//to do: find new cotrol to know available ?
					b_fight_touched = false;
					for(int i = 0; i < i_touched_pointer_count; i ++){
						if((e.getX(i) < I_SCREEN_WIDTH/2) && (e.getActionIndex() != i_fight_touched_pointerindex))
						i_fight_touched_pointerindex = e.getActionIndex();
						b_fight_touched = true;
						break;
					}
				}
			}		
			
			// if(i_move_touched_pointerindex == e.getActionIndex()){
				// i_move_touched_pointerindex = -1;
				// i_move_touched_pointerindex = -1;							
				// b_move_touched = false;
			// }
			// if(i_fight_touched_pointerindex ==e.getActionIndex()){
				// i_fight_touched_pointerindex = -1;
				// i_fight_touched_pointerid = -1;
				
				// b_fight_touched = false;
			// }
			// if(!b_move_touched){
				// i_move_small_circle_x = I_STATIC_MOVE_SMALL_CIRCLE_X;
				// i_move_small_circle_y = I_STATIC_MOVE_SMALL_CIRCLE_Y;
			// }
			// if(!b_fight_touched){
				// i_fight_small_circle_x = I_STATIC_FIGHT_SMALL_CIRCLE_X;
				// i_fight_small_circle_y = I_STATIC_FIGHT_SMALL_CIRCLE_Y;			
			// }
			//to do: find new control
		}
		if(e.getActionMasked() == MotionEvent.ACTION_DOWN){
			downtime ++;
			pointercountwhenpointerdown = e.getPointerCount();
			// System.out.println("Downnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");			
			//get pointer ID if have new pointer down
			
			for(int i = i_touched_pointer_count -1; i >= 0; i--){
				// if(e.getX(i) < I_SCREEN_WIDTH/2 && !b_move_touched){
				if((e.getX(i) < I_SCREEN_WIDTH/2) && (i_move_touched_pointerid == -1)){
					// if(e.getActionIndex() != i_fight_touched_pointerindex){
						// is_set_move = true;
						i_move_touched_pointerid = e.getPointerId(e.getActionIndex());
					// }else
						// is_set_move = false;
					// b_move_touched = true;
				// }else if(e.getX(i) >= I_SCREEN_WIDTH/2 && b_fight_touched){
				}
				if((e.getX(i) >= I_SCREEN_WIDTH/2) && (i_fight_touched_pointerid == -1)){
					// if(e.getActionIndex() != i_move_touched_pointerindex){
						// is_set_fight = true;
						i_fight_touched_pointerid = e.getPointerId(e.getActionIndex());
					// }else
						// is_set_fight = false;
					// b_fight_touched = true;
				}			
			}
			
			
			for(int i = 0; i < i_touched_pointer_count; i++){
				// if((e.getX(i) < I_SCREEN_WIDTH/2) && (i_move_touched_pointerid == -1) && e.getPointerId(e.getActionIndex()) != i_fight_touched_pointerid){
				if((e.getX(i) < I_SCREEN_WIDTH/2) && (i_move_touched_pointerid == -1)){
					i_move_touched_pointerid = e.getPointerId(e.getActionIndex());
					break;
				}
			}
			if(i_move_touched_pointerid != -1){
				fightactionindex = e.getActionIndex();
				fightactionid = e.getPointerId(e.getActionIndex());
			}
			for(int i = 0; i < i_touched_pointer_count; i++){
				// if((e.getX(i) >= I_SCREEN_WIDTH/2) && (i_fight_touched_pointerid == -1) && e.getPointerId(e.getActionIndex()) != i_move_touched_pointerid){
				if((e.getX(i) >= I_SCREEN_WIDTH/2) && (i_fight_touched_pointerid == -1)){
					i_fight_touched_pointerid = e.getPointerId(e.getActionIndex());					
					break;
				}
			}
			
			//get back pointer index belong to pointer ID
			i_move_touched_pointerindex = -1;
			i_fight_touched_pointerindex = -1;
			for(int i = 0; i < i_touched_pointer_count; i++){
				if(i_move_touched_pointerid == e.getPointerId(e.getActionIndex())){
					i_move_touched_pointerindex = i;
					break;
				}
			}
			for(int i = 0; i < i_touched_pointer_count; i++){
				if(i_fight_touched_pointerid == e.getPointerId(e.getActionIndex())){
					i_fight_touched_pointerindex = i;
					break;
				}
			}
			
			// if(i_move_touched_pointerindex != -1 && i_move_touched_pointerindex	== e.getActionIndex()){
			// if(e.getX() < I_SCREEN_WIDTH/2){
				//danh.ngothanh: check control move
			if(i_move_touched_pointerindex != -1){
					// System.out.println(" i_move_touched_pointerindex ==================== " + i_move_touched_pointerindex);
					// i_move_touched_pointerindex = e.getActionIndex();				
					f_move_touched_posx = e.getX(i_move_touched_pointerindex);
					f_move_touched_posy = e.getY(i_move_touched_pointerindex);
					f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));				
					if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
						f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
					}
					f_move_angle = Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y);
					i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(f_move_angle));
					i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(f_move_angle));									
					f_mc_posx += (float)(f_move_distance_length/7*Math.sin(f_move_angle));
					f_mc_posy += (float)(f_move_distance_length/7*Math.cos(f_move_angle));
					f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
					f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
				// }
			}
			// if(i_fight_touched_pointerindex != -1 && i_fight_touched_pointerindex == e.getActionIndex()){
			// if(e.getX() >= I_SCREEN_WIDTH/2){
				//danh.ngothanh: check control fight
			// if(b_fight_touched){
			if(i_fight_touched_pointerindex != -1){
					// System.out.println(" i_fight_touched_pointerindex =================================== " + i_fight_touched_pointerindex);
					// i_fight_touched_pointerindex = e.getActionIndex();
					f_fight_touched_posx = e.getX(i_fight_touched_pointerindex);
					f_fight_touched_posy = e.getY(i_fight_touched_pointerindex);				
					f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
					if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
						f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
					}
					f_gun_angle = Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y);
					i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(f_gun_angle));
					i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(f_gun_angle));
					f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
					f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
				// }
			}				
		}
		if(e.getActionMasked() == MotionEvent.ACTION_MOVE){
			// for(int i = i_touched_pointer_count -1; i >= 0; i--){
				// if(e.getX(i) < I_SCREEN_WIDTH/2 && !b_move_touched){
				// if((e.getX(i) < I_SCREEN_WIDTH/2) && (i_move_touched_pointerid == -1)){
					// if(e.getActionIndex() != i_fight_touched_pointerindex){
						// is_set_move = true;
						// i_move_touched_pointerid = e.getPointerId(e.getActionIndex());
					// }else
						// is_set_move = false;
					// b_move_touched = true;
				// }else if(e.getX(i) >= I_SCREEN_WIDTH/2 && b_fight_touched){
				// }
				// if((e.getX(i) >= I_SCREEN_WIDTH/2) && (i_fight_touched_pointerid == -1)){
					// if(e.getActionIndex() != i_move_touched_pointerindex){
						// is_set_fight = true;
						// i_fight_touched_pointerid = e.getPointerId(e.getActionIndex());
					// }else
						// is_set_fight = false;
					// b_fight_touched = true;
				// }
			// }
			// System.out.println("Moveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
			// for(int i = i_touched_pointer_count -1; i >= 0; i--){
				// if(e.getX(i) < I_SCREEN_WIDTH/2 && !b_move_touched){
				// if(e.getX(i) < I_SCREEN_WIDTH/2){					
					// if(e.getActionIndex() != i_fight_touched_pointerindex){
						// is_set_move = true;
						// i_move_touched_pointerindex	= e.getActionIndex();
					// }else
						// is_set_move = false;
					// b_move_touched = true;
				// }else if(e.getX(i) >= I_SCREEN_WIDTH/2 && b_fight_touched){
				// }else if(e.getX(i) >= I_SCREEN_WIDTH/2){
					// if(e.getActionIndex() != i_move_touched_pointerindex){
						// is_set_fight = true;
						// i_fight_touched_pointerindex = e.getActionIndex();
					// }else
						// is_set_fight = false;
					// b_fight_touched = true;
				// }
			// }

			// System.out.println("i_move_touched_pointerindex ================================= " + i_move_touched_pointerindex);
			// System.out.println("i_fight_touched_pointerindex ================================= " + i_fight_touched_pointerindex);
			// if(e.getX() < I_SCREEN_WIDTH/2){
				// i_move_touched_pointerindex	= e.getActionIndex();
				// b_move_touched = true;	
			// }				
			// if(e.getX() >= I_SCREEN_WIDTH/2){
				// i_fight_touched_pointerindex = e.getActionIndex();
				// b_fight_touched = true;
			// }
			
			// if(i_move_touched_pointerindex == e.getActionIndex()){
			// if(i_move_touched_pointerindex != -1 && i_move_touched_pointerindex	== e.getActionIndex()){
			
			//get back pointer index belong to pointer ID
			i_move_touched_pointerindex = -1;
			i_fight_touched_pointerindex = -1;
			for(int i = 0; i < i_touched_pointer_count; i++){
				if(i_move_touched_pointerid == e.getPointerId(e.getActionIndex())){
					i_move_touched_pointerindex = i;
					break;
				}
			}
			for(int i = 0; i < i_touched_pointer_count; i++){
				if(i_fight_touched_pointerid == e.getPointerId(e.getActionIndex())){
					i_fight_touched_pointerindex = i;
					break;
				}
			}
			
			if(i_move_touched_pointerindex != -1){
			// if(b_move_touched){
				//danh.ngothanh: check control move	
				// System.out.println(" drag i_move_touched_pointerindex ==================== " + i_move_touched_pointerindex);
				f_move_touched_posx = e.getX(i_move_touched_pointerindex);
				f_move_touched_posy = e.getY(i_move_touched_pointerindex);				
				f_move_distance_length = Math.sqrt((f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)*(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X)+(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y)*(f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y));				
				if(f_move_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
					f_move_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
				}
				f_move_angle = Math.atan2(f_move_touched_posx - I_STATIC_MOVE_SMALL_CIRCLE_X, f_move_touched_posy - I_STATIC_MOVE_SMALL_CIRCLE_Y);
				i_move_small_circle_x = (float)(I_STATIC_MOVE_SMALL_CIRCLE_X + f_move_distance_length*Math.sin(f_move_angle));
				i_move_small_circle_y = (float)(I_STATIC_MOVE_SMALL_CIRCLE_Y + f_move_distance_length*Math.cos(f_move_angle));									
				f_mc_posx += (float)(f_move_distance_length/7*Math.sin(f_move_angle));
				f_mc_posy += (float)(f_move_distance_length/7*Math.cos(f_move_angle));
				f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
				f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
			}
			// else if(i_fight_touched_pointerindex == e.getActionIndex()){
			// if(i_fight_touched_pointerindex != -1 && i_fight_touched_pointerindex == e.getActionIndex()){
			if(i_fight_touched_pointerindex != -1){
			// if(b_fight_touched){			
				//danh.ngothanh: check control fight
				// System.out.println(" drag i_fight_touched_pointerindex =================================== " + i_fight_touched_pointerindex);
				f_fight_touched_posx = e.getX(i_fight_touched_pointerindex);
				f_fight_touched_posy = e.getY(i_fight_touched_pointerindex);	
				f_fight_distance_length = Math.sqrt((f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)*(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X)+(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y)*(f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y));
				if(f_fight_distance_length > I_STATIC_SMALL_CIRCLE_RADIUS*2){
					f_fight_distance_length = I_STATIC_SMALL_CIRCLE_RADIUS*2;
				}
				f_gun_angle = Math.atan2(f_fight_touched_posx - I_STATIC_FIGHT_SMALL_CIRCLE_X, f_fight_touched_posy - I_STATIC_FIGHT_SMALL_CIRCLE_Y);
				i_fight_small_circle_x = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_X + f_fight_distance_length*Math.sin(f_gun_angle));
				i_fight_small_circle_y = (float)(I_STATIC_FIGHT_SMALL_CIRCLE_Y + f_fight_distance_length*Math.cos(f_gun_angle));
				f_gun_posx = (float)((double)f_mc_posx + i_mc_gun_length*Math.sin(f_gun_angle));
				f_gun_posy = (float)((double)f_mc_posy + i_mc_gun_length*Math.cos(f_gun_angle));
			}
		}
		*/
		// i_touched_pointer_count = e.getPointerCount();
		input_pointer_array = new int[i_touched_pointer_count];
		input_pointer_array_id = new int[i_touched_pointer_count];
		input_pointer_array_x = new float[i_touched_pointer_count];
		input_pointer_array_y = new float[i_touched_pointer_count];
		for(int i = 0; i < i_touched_pointer_count; i ++){
			input_pointer_array_id[i] = e.getPointerId(i);
			input_pointer_array_x[i] = e.getX(i);
			input_pointer_array_y[i] = e.getY(i);
		}
		if(f_mc_posx - i_mc_radius <= 0)
			f_mc_posx = i_mc_radius;
		if(f_mc_posx + i_mc_radius >= I_SCREEN_WIDTH)
			f_mc_posx = I_SCREEN_WIDTH - i_mc_radius;
		if(f_mc_posy - i_mc_radius <= 0)
			f_mc_posy = i_mc_radius;
		if(f_mc_posy + i_mc_radius >= I_SCREEN_HEIGHT)
			f_mc_posy = I_SCREEN_HEIGHT - i_mc_radius;
		return true;
		// return super.onTouchEvent(e);
	}	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Initialize();
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder){
		boolean retry = true;
		i_thread_can_run = false;
        do {
            try {
                o_game_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);
		
		// o_game_mainthread.stop();
		// o_game_mainthread.interrupt();
		// o_game_mainthread = null;
		// }while(o_game_mainthread != null);
		// if(o_game_mainthread.isAlive()){
			// System.out.println("isAliveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
			// o_game_mainthread.interrupt();			
		// }
		// do{
			// o_game_mainthread.interrupt();			
		// }while(o_game_mainthread.isAlive());
		
	}
	@Override
	public void run(){
		while(i_thread_can_run){
			Game_Update();
			Canvas o_lockcanvas = o_surfaceholder.lockCanvas(null);
			synchronized (o_surfaceholder) {
				if(o_lockcanvas != null){
					onDraw(o_lockcanvas);
					o_surfaceholder.unlockCanvasAndPost(o_lockcanvas);
				}
			}
			
		}
	}
	/*
	design:
	khi ban trung object, object se doi mau dam nhat de tao hieu ung, 
	tao hieu ung trung dan nhu pewpew
	moi lan ban trung se duoc 1 diem
	duoc 100 diem se +1 missile laught
	moi lan laught se co nhieu missile xuat phat tu vong quanh MC
	lam hieu ung lock taget neu duoc
	neu tren man hinh het target thi missiles se mat dieu khien va di chuyen thang, neu tren duong di ma gap mat cu object nao cung se no
	*/
}